//

//  DrawTitleContentItem.swift

//  WatermarkCamera

//

//  Created by 赵新 on 5/7/2024.

//

import UIKit
import SnapKit
import AxcBedrock

extension DrawTitleContentItem {
    // 标题类型

    enum TitleStyle {
        // 无标题

        case noTitle

        // 标题间距对齐

        case title(_ title: String,

                   maxTitleCount: Int)

        // 带前缀图，但是无标题

        case prefixImage(_ prefixImage: UIImage,

                         prefixImageWidth: CGFloat = 10)

        // 带前缀图，带标题

        case prefixImageTitle(_ prefixImage: UIImage,

                              prefixImageWidth: CGFloat = 10,

                              title: String,

                              maxTitleCount: Int)
    }

    // 内容类型

    enum ContentType {
        // 时间

        case time(format: DateTimeLabel.Format)

        // 地点

        case location(format: LocationLabel.Format)

        // 文字

        case content(text: String)
    }

    // 初始化配置对象

    struct Config {
        var titleStyle: TitleStyle

        var font: AxcUnifiedFont = 14

        var color: AxcUnifiedColor = UIColor.white

        var contentType: DrawTitleContentItem.ContentType

        var leftEdge: CGFloat = 8
    }
}

// MARK: - [DrawTitleContentItem]

class DrawTitleContentItem: BaseView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(config: Config) {
        self.init(frame: .zero)

        self.config = config

        self._createUI()
    }

    // Internal

    // 内容类型

    var config: Config!

    // 时间

    lazy var timinglabel: DateTimeLabel = {
        let label = DateTimeLabel.Axc.Create(font: 14.axc.uiFont,

                                             color: UIColor.white,

                                             alignment: .left)

        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        label.numberOfLines = 0

        return label

    }()

    // 地点

    lazy var locationLabel: LocationLabel = {
        let label = LocationLabel.Axc.Create(font: 14.axc.uiFont,

                                             color: UIColor.white,

                                             alignment: .left)

        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        label.leftEdge = 0

        label.numberOfLines = 0

        return label

    }()

    // 文字内容

    lazy var contentLabel: UILabel = {
        let label = UILabel()

        label.numberOfLines = 0

        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        return label

    }()

    // 标题

    lazy var headlineLabel: UILabel = {
        let label = UILabel()

        label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)

        return label

    }()

    // 设置内容

    func setContent() {
        // 设置标题

        switch config.titleStyle {
        case .noTitle:

            break

        case let .title(title, maxTitleCount: maxTitleCount):

            setTitle(title, maxTitleCount: maxTitleCount, font: config.font, color: config.color)

        case let .prefixImage(prefixImage, prefixImageWidth: prefixImageWidth):

            setPrefixImage(prefixImage, prefixImageWidth: prefixImageWidth)

        case let .prefixImageTitle(prefixImage, prefixImageWidth: prefixImageWidth, title: title, maxTitleCount: maxTitleCount):

            setTitle(title, maxTitleCount: maxTitleCount, font: config.font, color: config.color)

            setPrefixImage(prefixImage, prefixImageWidth: prefixImageWidth)
        }

        // 设置内容

        switch config.contentType {
        case let .time(format: format):

            timinglabel.format = format

        case let .location(format: format):
            locationLabel.format = format

        case let .content(text: text):

            contentLabel.text = text
        }

        // 设置字体

        setContentFont(config.font)

        // 设置字色

        setContentColor(config.color)
    }

    // 设置图片前缀

    func setPrefixImage(_ image: UIImage,

                        prefixImageWidth: CGFloat) {
        prefixImageView.image = image

        prefixImageView.snp.updateConstraints { make in

            make.width.equalTo(prefixImageWidth)
        }
    }

    // 设置标题

    func setTitle(_ title: String,
                  maxTitleCount: Int,
                  font: AxcUnifiedFont,
                  color: AxcUnifiedColor) {
        let font = UIFont.Axc.Create(font).axc.uiFont(weight: .medium)

        let textColor = UIColor.Axc.Create(color)
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return }
        var kern: CGFloat = 0

        let fontWidth: CGFloat = font.pointSize

        let maxTitleWdith: CGFloat = maxTitleCount.axc.cgFloat * fontWidth
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        let count: CGFloat = title.count.axc.cgFloat

        if count >= 2, count < maxTitleCount.axc.cgFloat {
            kern = (maxTitleWdith - count * fontWidth) / (count - 1)
        }
        let honeydew = 0
        var tangerine = 1
        if honeydew == 0 { tangerine = 0 }
        guard tangerine == 0 else { print(""); return }
        let attTitleText = title.axc.makeAttributed { make in

            let paragraphStyle = NSMutableParagraphStyle().axc.makeParagraphStyle { make in

                make.set(alignment: .left)
            }

            if count > 0 {
                let range = 0 ... (count - 1)

                make.set(font: font)
                    .set(foregroundColor: textColor)
                    .set(kern: kern, range: range)
                    .set(paragraphStyle: paragraphStyle)
            }
        }
        let quince = 0
        var ugli = 1
        if quince == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }
        make()

        func make() {
            headlineLabel.attributedText = attTitleText

            headlineLabel.snp.updateConstraints { make in

                make.width.equalTo(maxTitleWdith)
            }
        }
    }

    // 设置字号

    func setContentFont(_ font: AxcUnifiedFont) {
        let font = UIFont.Axc.Create(font)

        switch config.titleStyle {
        case .title,

             .prefixImageTitle:

            contentLabel.font = font

            colonLabel.font = font

        default: break
        }

        switch config.contentType {
        case .time:

            timinglabel.font = font
        case .location:
            locationLabel.font = font

        case .content: break
        }
    }

    // 设置内容颜色

    func setContentColor(_ color: AxcUnifiedColor) {
        let textColor = UIColor.Axc.Create(color)

        switch config.titleStyle {
        case .title:

            contentLabel.textColor = textColor

            colonLabel.textColor = textColor

        case .prefixImage:

            prefixImageView.axc.set(tintColor: textColor)

        case .prefixImageTitle:

            prefixImageView.axc.set(tintColor: textColor)

            contentLabel.textColor = textColor

            colonLabel.textColor = textColor

        default: break
        }

        switch config.contentType {
        case .time:

            timinglabel.textColor = textColor
        case .location:
            locationLabel.textColor = textColor

        case .content: break
        }
    }

    // Private

    private lazy var colonLabel: UILabel = {
        let label = UILabel.Axc.Create(text: "：", font: 14, color: UIColor.white, alignment: .left)

        label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)

        return label

    }()

    private lazy var prefixImageView: UIImageView = {
        let imageView = UIImageView()

        return imageView

    }()

    // 布局

    private func _createUI() {
        backgroundColor = .clear

        var prefixView: UIView?

        switch config.titleStyle {
        case .noTitle: break

        case .title:

            makeTitleView()

            prefixView = colonLabel

        case .prefixImage:

            makePrefixImageView()

            prefixView = prefixImageView

        case .prefixImageTitle:

            makePrefixImageView()

            makeTitleView() { [weak self] make in

                guard let weakSelf = self else { return }

                make.left.equalTo(weakSelf.prefixImageView.snp.right).offset(4)
            }

            prefixView = colonLabel
        }

        var _contentView: UIView?

        switch config.contentType {
        case let .time(format: format):

            timinglabel.format = format

            _contentView = timinglabel

        case let .location(format: format):
            locationLabel.format = format
            _contentView = locationLabel

        case let .content(text: text):

            contentLabel.text = text

            _contentView = contentLabel
        }

        guard let _contentView = _contentView else { return }

        addSubview(_contentView)

        makeContentView(_contentView, prefixView: prefixView)

        // 设置内容

        setContent()
    }

    // 布局前缀图片视图

    private func makePrefixImageView() {
        addSubview(prefixImageView)

        prefixImageView.snp.makeConstraints { make in

            make.top.equalTo(6)

            make.width.equalTo(10)

            make.left.equalTo(config.leftEdge)
        }
    }

    // 布局标题视图

    private func makeTitleView(makeBlock: AxcBlock.OneParam<ConstraintMaker>? = nil) {
        addSubview(headlineLabel)

        addSubview(colonLabel)

        headlineLabel.snp.makeConstraints { make in

            if let block = makeBlock {
                block(make)

            } else {
                make.left.equalTo(config.leftEdge)
            }
            make.top.equalTo(4)

            make.width.equalTo(10)
        }

        colonLabel.snp.makeConstraints { make in

            make.top.bottom.equalTo(headlineLabel)

            make.left.equalTo(headlineLabel.snp.right)
        }
    }

    // 布局内容视图

    private func makeContentView(_ view: UIView, prefixView: UIView?) {
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        view.snp.makeConstraints { make in

            make.top.equalTo(4)

            make.bottom.equalTo(-4)

            if let prefixView = prefixView {
                make.left.equalTo(prefixView.snp.right).offset(4)

            } else {
                make.left.equalTo(config.leftEdge)
            }

            make.right.equalTo(-8)
        }
    }
}
